<template>
    <div>
        <van-tabs v-model="active" @click="clickTab" class="new-list" :swipe-threshold="5">
            <van-tab title="全部"></van-tab>
            <van-tab title="待付款"></van-tab>
            <van-tab title="待发货"></van-tab>
            <van-tab title="待收货"></van-tab>
            <van-tab title="已收货"></van-tab>
        </van-tabs>
        <div class="order-list">
            <div v-for="(good, key) in orderList" :key="key" class="cell">
                <div @click="toDetail(good.id, good.status)">
                    <div class="top">
                        <div class="number">
                            <svg class="icon lq" aria-hidden="true" v-if="good.type==='2'">
                                <use xlink:href="#icon-user-lq"></use>
                            </svg>
                            订单号：{{good.id}}
                        </div>
                        <div>{{good.status | orderState}}</div>
                    </div>
                    <div class="g-body">
                        <order-goods :orderGoods="good.products" :type="type"></order-goods>
                    </div>
                    <div class="total">
                        <div>数量：{{good.num}}</div>
                        <div>合计：{{good.money | moneyFmt(2, '￥')}}</div>
                    </div>
                    <div class="line"></div>
                </div>
                <order-btn :good="good" type="list" @finishCancel="finish"/>
            </div>
            <infinite-loading  :identifier="infiniteId"
                               @infinite="infiniteHandler"
                               spinner="circles"
            >
                <div class="no-more" slot="no-more">没有更多了</div>
                <div class="no-more" slot="no-results">暂时没有相关订单</div>
            </infinite-loading>
        </div>

    </div>
</template>

<script>
    import API from '../../../request/api'
    import OrderBtn from "../../../components/User/orderbtn";
    import OrderGoods from "../../../components/User/order-goods";
    import {commonMixin} from '@/common/mixin';

    export default {
        components: {
            OrderGoods,
            OrderBtn
        },
        mixins: [commonMixin],
        data() {
            return {
                pageIndex: 1,
                active: 0,
                infiniteId: +new Date(), // 该值改变后重新调用handler方法
                loading: false,
                finished: [],
                pages: [],
                orderList: [],
                type: 1,
                system: this.$store.state.login.system,
            };
        },
        computed: {
            status(){
                return this.active === 0 ? 10 : this.active - 1;
            }
        },
        created() {
            this.active = parseInt(this.$route.query.active);
            this.showGlobaLoading(); //打开loading
        },
        watch:{
            active(){
                this.pageIndex = 1;
                this.orderList = [];
                this.infiniteId += 1;
                this.showGlobaLoading(); //打开loading
            }
        },
        methods: {
            finish(id){
                // this.orderList.filter((item,index)=>{
                //     if(item.id === id){
                //         this.orderList.splice(index,1)
                //     }
                // })
                // this.orderList.filter((item,index)=>{
                //     if(item.id === id){
                //         this.orderList.splice(index,1)
                //     }
                // })
                this.$toast({
                    message: '取消成功',
                    duration: 1000
                });
                setTimeout(()=>{
                    location.reload()
                }, 1000)
            },
            clickTab(index){
                this.active = index;
            },
            toDetail(val, type){
                this.$router.push({
                    name: 'orderDetail',
                    query: {
                        ID: val,
                        type: type
                    }
                });
            },
            infiniteHandler($state){
                setTimeout(() => {
                    this.request.get(API.orderList, {
                        status: this.status,
                        type: 1,
                        page: this.pageIndex
                    }).then((res) => {
                        if (res.list.length) {
                            this.orderList.push(...res.list);
                            if(res.list.length < 20){
                                $state.complete();
                            }
                            this.pageIndex += 1;
                            $state.loaded();
                        } else {
                            $state.complete();
                        }
                        this.hideGlobaLoading(); //关闭loading
                    }).catch((err) => {
                        console.error('orderList-err', err);
                    });
                },500)
            },
            onLoad(){
                if (this.finished[this.active] === false) {
                    console.log(this.active);
                    // 异步更新数据
                    let data = {
                        status: this.status,
                        type: 1,
                        page: this.pages[this.active]
                    };
                //     // this.receive = true;
                    this.request.get(API.orderList,data).then(res => {
                        this.pages[this.active]++;
                        console.log('this.pages[this.active]++;',this.pages[this.active]++)
                        let newData = this.orderList[this.active].concat(res.list);
                        this.$set(this.orderList,this.active,newData);
                        if (res.total_page < this.pages[this.active]) {
                            this.finished[this.active] = true;
                        }
                        this.loading = false;
                    }, () => {
                    });
                }
            }
        },
        beforeCreate() {
            // 可以在这加个loading事件，在加载实例时触发
            // 这个阶段实例的data、methods是读不到的
        },
        beforeMount() {
            // 相关的 render 函数首次被调用。
        },
        mounted() {
            // 挂载元素，获取到DOM节点
            // el选项的DOM节点 被新创建的 vm的el 替换，并挂载到实例上去之后调用此生命周期函数。此时实例的数据在DOM节点上进行渲染
        },
        beforeUpdate() {
            // 数据更新时调用，但不进行DOM重新渲染，在数据更新时DOM没渲染前可以在这个生命函数里进行状态处理
        },
        updated() {
            //  如果对数据统一处理，在这里写上相应函数
            // 这个状态下数据更新并且DOM重新渲染，当这个生命周期函数被调用时，组件 DOM 已经更新，所以你现在可以执行依赖于 DOM 的操作。当实例每次进行数据更新时updated都会执行
        },
        beforeDestroy() {
            // 可以做一个确认停止事件的确认框
        },
        destroyed() {
            // Vue 实例销毁后调用。调用后，Vue 实例指示的所有东西都会解绑定，所有的事件监听器会被移除，所有的子实例也会被销毁
        }
    };

</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
    @import '~assets/main.styl'
    .new-list{
          position fixed
          z-index 99
          top 0
          width 100%
      }
      .order-list{
        margin-top vw(55)
      }
    .cell{
        margin-top vw(10)
        background-color grayF
        padding 0
        .top{
            display flex
            align-items center
            justify-content space-between
            font-size word12
            color main-color
            padding cellPadding cellPadding 0
            .number{
                display flex
                align-items center
                color gray3
                .lq{
                    width vw(24)
                    height vw(24)
                    fill #F8BE43
                    margin-right titleMargin
                }
            }
        }
        .total{
            display flex
            align-items center
            justify-content space-between
            padding 0 cellPadding
        }
        .line{
            width 100vw
            height vw(1)
            background-color #f8f8f8
        }
    }
    .no-more{
        text-align center
        color gray9
        line-height vw(30)
        margin-top smallMargin
    }
</style>
